﻿@{
}

<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="">
    <div style="padding: 20px;">
        <h3><i class="layui-icon layui-icon-add-1"></i> 新增物品</h3>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">物品编号</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsCode" autocomplete="off" class="layui-input" id="goodsCode" readonly />
            </div>
            <div class="layui-input-inline" style="width: 80px;">
                <button type="button" class="layui-btn layui-btn-sm" onclick="generateGoodsCode()">生成编号</button>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">物品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsName" autocomplete="off" class="layui-input" />
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsModel" autocomplete="off" class="layui-input" />
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-inline">
               <select name="unit" lay-filter="responsible">
                <option value="">请选择单位</option>
                <option value="个">个</option>
                <option value="包">包</option>
                <option value="件">件</option>
                <option value="千克">千克</option>
                <option value="张">张</option>
                <option value="块">块</option>
            </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">物品类型</label>
            <div class="layui-input-inline">
                <select name="goodsType" lay-filter="responsible">
                <option value="">请选择类型</option>
                <option value="化工类">化工类</option>
                <option value="合金类">合金类</option>
                <option value="食品类">食品类</option>
                    <option value="天然类">天然类</option>
            </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">最小库存</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="minStock" autocomplete="off" class="layui-input" />
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">最大库存</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="maxStock" autocomplete="off" class="layui-input" />
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">当前库存</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="stock" autocomplete="off" class="layui-input" />
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="price" autocomplete="off" class="layui-input" />
            </div>
        </div>
    </div>

    <div class="layui-form-item" pane>
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="true" title="启用" checked>
            <input type="radio" name="status" value="false" title="禁用">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="goodsSubmit">提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 页面加载完成后自动生成编号
        $(document).ready(function() {
            generateGoodsCode();
        });

        // 自动生成物品编号
        function generateGoodsCode() {
            var currentDate = new Date();
            var year = currentDate.getFullYear();
            var month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
            var day = currentDate.getDate().toString().padStart(2, '0');
            var sequence = (Math.floor(Math.random() * 9000) + 1000).toString(); // 生成4位随机序号
            var goodsCode = 'G' + year + month + day + sequence;
            $('#goodsCode').val(goodsCode);
        }

        form.on('submit(goodsSubmit)', function (data) {
            var f = data.field;
            var $btn = $('button[lay-submit]');
            if ($btn.prop('disabled')) return false;
            $btn.prop('disabled', true).text('提交中...');

            var request = {
                goodsCode: (f.goodsCode || '').trim(),
                goodsName: (f.goodsName || '').trim(),
                goodsModel: (f.goodsModel || '').trim(),
                unit: (f.unit || '').trim(),
                goodsType: (f.goodsType || '').trim(),
                minStock: parseFloat(f.minStock || 0),
                maxStock: parseFloat(f.maxStock || 0),
                stock: parseFloat(f.stock || 0),
                price: parseFloat(f.price || 0),
                status: f.status === 'true',
                remark: (f.remark || '').trim()
            };

            $.ajax({
                url: 'http://localhost:5141/api/Goods/AddGoodsinfo',
                type: 'POST',
                data: JSON.stringify(request),
                contentType: 'application/json',
                success: function (res) {
                    $btn.prop('disabled', false).text('提交');
                    if (res && (res > 0 || res.code === 200)) {
                        layer.msg('添加成功', { icon: 1 });
                        $('form')[0].reset();
                        form.render();
                         if (window.parent && window.parent.loadPage) {
                                window.parent.loadPage('/Bom/GoodsLists', '物品列表');
                         }
                    } else {
                        layer.msg('添加失败', { icon: 2 });
                    }
                },
                error: function () {
                    $btn.prop('disabled', false).text('提交');
                    layer.msg('网络错误', { icon: 2 });
                }
            });
            return false;
        });
    });
</script>
